package reflect;

import java.lang.reflect.Field;

public class TestReflect {

    public static void main(String[] args) {
        User user = new User();
        user.setName("aa");
        user.setAge(12);
//        System.out.println(user.toString());
//        try {
//            Class<User> userClass = User.class;
//            User instance = userClass.newInstance();
//            Field name = userClass.getDeclaredField("name");
//            name.setAccessible(true);
//            Object o = name.get(user);
//            System.out.println(o);
//            name.set(instance,"bb");
//            System.out.println(instance.toString());
//            User user2 = instance;
//            user2.setAge(199);
//            System.out.println(user2.toString());
//        } catch (InstantiationException e) {
//            e.printStackTrace();
//        } catch (IllegalAccessException e) {
//            e.printStackTrace();
//        } catch (NoSuchFieldException e) {
//            e.printStackTrace();
//        }

        TestReflect testReflect = new TestReflect();
        testReflect.test(user);

//        User user = new User();
//        user.setName("aa");
//        user.setAge(12);

    }

    public void test(Object o){
        try {
            Class<?> aClass = o.getClass();
            for (Field field : aClass.getDeclaredFields()) {
                field.setAccessible(true);
                System.out.println(field.get(o));
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

}
